package com.citybox.shared.web.page;

import com.citybox.type.constant.HttpStatus;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 */
@Data
public class TableDataInfo<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 消息状态码
     */
    private int code = HttpStatus.SUCCESS;

    /**
     * 消息内容
     */
    private String msg = "操作成功";

    /**
     * 总记录数
     */
    private long total;

    /**
     * 列表数据
     */
    private List<T> rows;

    public TableDataInfo() {
    }

    public TableDataInfo(List<T> list, long total) {
        this.rows = list;
        this.total = total;
    }

    public TableDataInfo(List<T> list, long total, int code, String msg) {
        this.rows = list;
        this.total = total;
        this.code = code;
        this.msg = msg;
    }

    // 成功方法
    public static <T> TableDataInfo<T> success(List<T> list, long total) {
        return new TableDataInfo<>(list, total);
    }

    public static <T> TableDataInfo<T> success(String msg) {
        TableDataInfo<T> result = new TableDataInfo<>();
        result.setMsg(msg);
        return result;
    }

    // 错误方法
    public static <T> TableDataInfo<T> error(String msg) {
        TableDataInfo<T> result = new TableDataInfo<>();
        result.setCode(HttpStatus.ERROR);
        result.setMsg(msg);
        return result;
    }

    public static <T> TableDataInfo<T> error(int code, String msg) {
        TableDataInfo<T> result = new TableDataInfo<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}